var express = require("express");
var app = express();
var mysql = require("mysql");
var Mock = require("mockjs");
var fs = require("fs");
// app.get("/img", function (req, res) {
//     res.setHeader("Access-Control-Allow-Origin", "*")
//     var connection = mysql.createConnection({
//         host: "localhost",
//         user: "root",
//         password: "root",
//         database: "dmw"
//     })
//     connection.connect()
//     var str = 'SELECT * FROM `dm_image` where id < 30 '
//     // var newStr = [req.query.itemType1Name]
//     connection.query(str, function (err, data) {
//         if (err) {
//             console.error(err)
//         }
//         res.send(data)
//     })
// });

app.get("/imgUrl", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id > 39 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});

app.get("/vul", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT itemName,minPrice FROM `dm_item` where id < 7 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});

app.get("/next_pic", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id > 37 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/next2_pic", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id = 4 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/erji", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id >30 and id <38'
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/youce", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT itemName,startTime,itemType2Name,minPrice FROM `dm_item` where id =1415'
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/last", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id >26 and id <36'
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
// -------------------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------------

app.get("/imgUrl_tiyu", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id > 36 and id < 45'
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});

app.get("/vul_tiyu", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT itemName,minPrice FROM `dm_item` where id > 7 and id < 14 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});

app.get("/next_pic_tiyu", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id > 33 and id < 42 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/next2_pic_tiyu", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id = 4 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/erji_tiyu", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id >27 and id <36'
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/youce_tiyu", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT itemName,startTime,itemType2Name,minPrice FROM `dm_item` where id =1414'
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/last_tiyu", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id >29 and id <40'
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});

// -------------------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------------

app.get("/imgUrl_yaogun", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id < 39 and id >32 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});

app.get("/vul_yaogun", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT itemName,minPrice FROM `dm_item` where id > 7 and id < 14 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});

app.get("/next_pic_yaogun", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id > 33 and id < 43 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/next2_pic_yaogun", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id = 4 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/erji_yaogun", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id >36 and id <46'
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/youce_yaogun", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT itemName,startTime,itemType2Name,minPrice FROM `dm_item` where id =1413'
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/last_yaogun", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id >30 and id <39'
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});

// -------------------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------------

app.get("/imgUrl_xiju", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id > 3 and id < 29 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});

app.get("/vul_xiju", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT itemName,minPrice FROM `dm_item` where id > 15 and id <22 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});

app.get("/next_pic_xiju", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id < 34 and id > 5 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/next2_pic_xiju", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id = 4 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/erji_xiju", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id >34 and id < 41'
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/youce_xiju", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT itemName,startTime,itemType2Name,minPrice FROM `dm_item` where id =1412'
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/last_xiju", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id >4 and id < 31'
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});

// -------------------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------------


app.get("/imgUrl_wudao", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id > 39 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});

app.get("/vul_wudao", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT itemName,minPrice FROM `dm_item` where id > 55 and id < 62 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});

app.get("/next_pic_wudao", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id > 33 and id < 44 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/next2_pic_wudao", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id = 4 '
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/erji_wudao", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id >38 and id <48'
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/youce_wudao", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT itemName,startTime,itemType2Name,minPrice FROM `dm_item` where id =1413'
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});
app.get("/last_wudao", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "dmw"
    })
    connection.connect()
    var str = 'SELECT imgUrl FROM `dm_image` where id >35 and id <27'
    // var newStr = [req.query.itemType1Name]
    connection.query(str, function (err, data) {
        if (err) {
            console.error(err)
        }
        res.send(data)
    })
});

// 登录验证账号
app.get("/login", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        // 数据库地址
        host: "localhost",
        // 用户名
        user: "root",
        // 密码
        password: "123456",
        // 数据库名称
        database: "dmw"
    })
    // 2. 打开链接 使用conect()函数
    connection.connect()

    console.log(req.query)
    // 3. 执行数据库操作行为
    var str = "SELECT * from dm_name where name = ? and pwd = ?";
    var newStr = [req.query.phone, req.query.password];
    connection.query(str, newStr, function (err, data) {
        // 判断错误
        if (err) {
            throw err;
        }
        // 渲染到页面
        res.send(data);
    })
})

// 修改密码验证账号
app.get("/chaxun", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({

        // 数据库地址
        host: "localhost",
        // 用户名
        user: "root",
        // 密码
        password: "123456",
        // 数据库名称
        database: "dmw"
    })
    // 2. 打开链接 使用conect()函数
    connection.connect()

    console.log(req.query)
    // 3. 执行数据库操作行为
    var str = "SELECT * from dm_name where name = ?";
    var newStr = [req.query.phone];
    connection.query(str, newStr, function (err, data) {
        // 判断错误
        if (err) {
            throw err;
        }
        // 渲染到页面
        res.send(data);
    })
})

// 修改密码
app.get("/xiugai", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({

        // 数据库地址
        host: "localhost",
        // 用户名
        user: "root",
        // 密码
        password: "123456",
        // 数据库名称
        database: "dmw"
    })
    // 2. 打开链接 使用conect()函数
    connection.connect()

    console.log(req.query)
    // 3. 执行数据库操作行为
    var str = "UPDATE dm_name set pwd = ? where name = ?";
    var newStr = [req.query.newPass, req.query.phone];
    connection.query(str, newStr, function (err, data) {
        // 判断错误
        if (err) {
            throw err;
        }
        // 渲染到页面
        res.send(data);
        console.log(1111)
    })
})

// 渲染轮播图信息
app.get("/imginfo", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "123456",
        database: "dmw"
    })
    // 使用connect函数打开链接
    connection.connect();
    // 执行数据库操作行为
    var str = "select imgURL from dm_image where id > 39";
    connection.query(str, function (err, data) {
        if (err) {
            throw error;
        }
        else {
            console.log(data)
        }
        res.send(data)
    })
})
// 图片列表
// app.get("/listinfo", function (req, res) {
//     res.setHeader("Access-Control-Allow-Origin", "*")
//     var connection = mysql.createConnection({
//         host: "localhost",
//         user: "root",
//         password: "123456",
//         database: "dmw"
//     })
//     // 使用connect函数打开链接
//     connection.connect();
//     // 执行数据库操作行为
//     var str = "select imgURL from dm_item where id < 7";
//     connection.query(str, function (err, data) {
//         if (err) {
//             throw error;
//         }
//         else {
//             console.log(data)
//         }
//         res.send(data)
//     })
// })
// 图片对应名字和价格
app.get("/nameinfo", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "123456",
        database: "dmw"
    })
    // 使用connect函数打开链接
    connection.connect();
    // 执行数据库操作行为
    var str = "SELECT imgURL,itemName,minPrice FROM `dm_item` where id<7";
    connection.query(str, function (err, data) {
        if (err) {
            throw error;
        }
        else {
            console.log(data)
        }
        res.send(data)
    })
})

// 时间，内容和价格
app.get("/allinfo", function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*")
    var connection = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "123456",
        database: "dmw"
    })
    // 使用connect函数打开链接
    connection.connect();
    // 执行数据库操作行为
    var str = "SELECT imgURL, itemName, createdTime,itemType1Name,minPrice FROM `dm_item` where id<5";
    connection.query(str, function (err, data) {
        if (err) {
            throw error;
        }
        else {
            console.log(data)
        }
        res.send(data)
    })
})

app.listen(8082, function () {
    console.log("服务器启动成功")
})